<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>

<%@ page import="com.conversationboard.model.*" %>
<%@ page import="com.conversationboard.view.*" %>
<%@ page import="java.security.Principal" %>
<%@ page import="com.conversationboard.config.*" %>
<%@ page import="com.conversationboard.site.*" %>
<%@ page import="com.conversationboard.authenticator.AuthenticationCookie"%>
<%@ page import="com.conversationboard.tree.Folders"%>
<%@ page import="com.conversationboard.tree.Node"%>
<%@ page import="java.util.List"%>

<% 
	Site.incrementPageHitCount();
	
    Principal principal = request.getUserPrincipal();
    String userName = null;
    
    if (principal != null) {
    	userName = principal.getName();
    } else {
    	
    	/* Try to automatically log you in, if you have login cookies, by forcing a redirect to the login page,
    	   which should log you in and pass you back here. */
		
    	if (AuthenticationCookie.getUserName(request) != null) {
    		response.sendRedirect(Configuration.getInstance().getRoot() + "/LoginControllerServlet?autologin=true");
    		return;
    	}
    }
   
 	User user = User.get(userName);
 	user.updateLastLoggedIn(IPAddress.getIPAddress(request));
 	Bookmarks bookmarks = user.getBookmarks();
	Node currentFolder = Folders.getInstance().getRoot();
	List<Node> nodes = currentFolder.getChildNodes(user, false);
	
 	/* Get a handle on the current folder */
 	
	if (request.getParameter("folderid") != null) {
		currentFolder = Folders.getInstance().getRoot().findFolder(Integer.parseInt(request.getParameter("folderid")));
		nodes = currentFolder.getChildNodes(user, false);
	}
 	
    request.setAttribute("user", user);
    request.setAttribute("nodes", nodes);
    request.setAttribute("currentFolder", currentFolder);
    request.setAttribute("numberOfUsers", Site.getUserCount());
    request.setAttribute("numberOfThreads", Site.getThreadCount());
    request.setAttribute("numberOfMessages", Site.getMessageCount());
    request.setAttribute("activeUsers", Site.getCurrentlyActiveUsers());
    
    request.setAttribute("config", Configuration.getInstance());
    
%>


<%@page import="com.conversationboard.controller.ipaddress.IPAddress"%><html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta name="ROBOTS" content="NOINDEX"></meta>
	<title><%=Configuration.getInstance().getSiteName()%></title>
	<link rel="stylesheet"title="Conversation Board" type="text/css" href="${contextPath}/Stylesheets/<%=StyleSheets.getStyleSheetFileName(request.getCookies())%>">
</head>

<body>

	<%= MainMenu.display(principal) %>

	<% if (SiteMessage.getMessage() != null) { %>	
		<div class='sitemessage'><%= SiteMessage.getMessage() %></div>
	<% } %>

	<% if (BatchMessage.getMessage() != null) { %>	
		<div class='batchmessage'><%= BatchMessage.getMessage() %></div>
	<% } %>
	
	<% if (user.isSiteAdmin() && (Site.getPendingRegistrations() > 0)) { %>
		<div class='batchmessage'><a href="${contextPath}/Pages/Admin/pendingregistrations.jsp"><%= Site.getPendingRegistrations() %> pending registration(s).</a></div>
	<% } %>

	<c:if test="${user.yellowCarded}">
		<div class="yellowcard">You Have Been Yellow Carded By a Moderator: ${user.yellowCardReason}</div>
	</c:if>

	<%= BookmarksDisplay.display(bookmarks, false) %>

	<c:if test="${siteshut}">
		<div class="sitemessage"><strong><span class="red">Site is currently shut. You are accessing as an administrator. Open the site in the admin menu option <a href="${contextPath}/Pages/Admin/openclose.jsp">Open/Close Site</a></span></strong></div>
	</c:if>
	
	<div class="headingblock">

		<!-- <div style="margin-bottom: 2em; background-color: #29A52E; height: 8em; width: 100%;"></div> -->

		<h1><%= Configuration.getInstance().getSiteName()%>
		<% if (Configuration.getInstance().isTestServer()) { %>
			(Test Server)
		<% } %>
		</h1>
		
		<c:if test="${not currentFolder.root}">
			<h2>${currentFolder.folderName}: ${currentFolder.folderDescription}</h2>
		</c:if>
	</div>

	<div class="mainblock">

		<p>
			<span class="leftmenulink"><strong><fmt:formatNumber groupingUsed="true">${numberOfUsers}</fmt:formatNumber></strong>  registered users</span>
			<span class="leftmenulink"><strong><fmt:formatNumber groupingUsed="true">${numberOfThreads}</fmt:formatNumber></strong> threads</span>
			<span class="leftmenulink"><strong><fmt:formatNumber groupingUsed="true">${numberOfMessages}</fmt:formatNumber></strong> messages</span>
			<!-- <span class="leftmenulink">(<a href="${contextPath}/FlexStats-debug/statistics.jsp">more statistics</a>)</span> -->
			<c:if test="${user.loggedIn}">
				<span class="leftmenulink">You have posted <strong><fmt:formatNumber groupingUsed="true">${user.postCount}</fmt:formatNumber></strong> messages</span>
			</c:if>
			<span class="leftmenulink"><strong><fmt:formatNumber groupingUsed="true">${activeUsers}</fmt:formatNumber></strong> logged in users active in the past five minutes</span>
		</p>
	
		<table class="table">

			<tr class="header">
				<td class="cell">Name</td>
				<td class="cell">Description</td>
				<td class="cell">Type</td>
				
				<c:if test="${user.siteAdmin}">
					<td class="cell">Administration</td>
				</c:if>
				
			</tr>

		    <c:forEach var="node" items="${nodes}">
				<tr class="row">
					<c:if test="${node.board}">

						<c:choose>
							<c:when test="${node.boardObject.popularBoard}">
								<td class="cell"><strong><a href="NewAnswersControllerServlet?boardid=${node.boardObject.boardId}">${node.boardObject.name}</a></strong></td>
							</c:when>
							<c:otherwise>
								<td class="cell"><a href="NewAnswersControllerServlet?boardid=${node.boardObject.boardId}">${node.boardObject.name}</a></td>
							</c:otherwise>
						</c:choose>
	
						<td class="cell">${node.boardObject.description}</td>
						
						<c:choose>
							<c:when test="${node.boardObject.privateBoard}">
								<td class="cell"><span class="red">Private</span></td>
							</c:when>
							<c:when test="${node.boardObject.inviteOnly}">
								<td class="cell"><span class="amber">Invite Only</span></td>
							</c:when>
							<c:otherwise>
								<td class="cell"><span class="green">Public</span></td>
							</c:otherwise>
						</c:choose>
	
						<c:if test="${user.siteAdmin}">
							<td class="cell">
								<a href="Pages/Admin/editboard.jsp?boardid=${node.boardObject.boardId}">Edit...</a>
							</td>
						</c:if>
					</c:if>
				</tr>
		    </c:forEach>

		    <c:forEach var="node" items="${nodes}">
				<c:if test="${node.externalLink}">
					<tr class="row">
						<td class="cell">${node.linkHtml}</td>
						<td class="cell">${node.linkDescription}</td>
						<td class="cell"><strong>Link</strong></td>
						<c:if test="${user.siteAdmin}">
							<td class="cell"></td>
						</c:if>
					</tr>
				</c:if>
			</c:forEach>

		    <c:forEach var="node" items="${nodes}">
				<c:if test="${node.folder}">
					<tr class="folderrow">
						<td class="cell"><a href="index.jsp?folderid=${node.nodeId}"><strong>[Folder]</strong> ${node.folderName}</a></td>
						<td class="cell">${node.folderDescription}</td>
						<td class="cell"><strong>Folder</strong></td>
						<c:if test="${user.siteAdmin}">
							<td class="cell"></td>
						</c:if>
					</tr>
				</c:if>
			</c:forEach>

		</table>

		<p>
			<c:if test="${user.siteAdmin or (user.loggedIn and config.allowUserBoardCreation)}">
				<a class="leftmenulink" href="Pages/createboard.jsp">Create a New Board</a>
			</c:if>
	
			<c:if test="${not currentFolder.root}">
				<a class="menulink" href="index.jsp?folderid=${currentFolder.parentNode.nodeId}">Go To Parent Folder</a>
			</c:if>
	
			<a class="menulink" href="flatlist.jsp">Flat List of Boards</a>
			<a class="menulink" href="${contextPath}/Pages/sitemap.jsp">View Site Map</a>
			<a class="menulink" href="${contextPath}/RandomThreadsControllerServlet">Random Threads</a>
			<a class="menulink" href="${contextPath}/Pages/most-read-threads.jsp">Most Read Threads</a>
	
			<c:if test="${config.allowUserBoardCreation and not user.loggedIn}">
				<p><strong>If you wish to create a new board, you must first be registered and logged in.</strong></p>
			</c:if>
		</p>
	

		<div class="adminmenulinks">
			<c:if test="${user.admin}">
				<a href="Pages/Admin/adminmenu.jsp">Administration Menu</a><br />
				<a href="<%=Configuration.getInstance().getRoot()%>/PerformanceStatsControllerServlet">Performance Statistics</a><br />
				<a href="<%=Configuration.getInstance().getRoot()%>/RecentActiveUsersControllerServlet">Recently Active Users</a><br />
			</c:if>
			<c:if test="${(user.loggedIn and config.displayAdminLogToUsers) or (user.siteAdmin and config.displayAdminLogToSiteAdmins)}">
					<a href="${contextPath}/ViewAdminLogControllerServlet?pageNumber=0">Admin Log</a><br />
			</c:if>
		</div>
		
	</div>
	
	<!--<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>-->
	<!--<script type="text/javascript">_uacct = "UA-1148679-1";urchinTracker();</script>--> 

</body>
</html>
